import * as counterService from '../services/query';

export  default {
  namespace: 'count',
  state: {
    count: 0,
    lastCount: 0,
    asyncData:['暂无'],
  },
  reducers: {
    add (count) {
      return {
        ...count,
        count: count.count + Math.random()*10,
        lastCount: count.count
      };
    },
    minus (count) {
      return {
        ...count,
        count: count.count - Math.random()*10,
        lastCount: count.count
      };
    },
    changeData (state, action) {
      console.log('state:', state);
      console.log('action:', action.payload.temp.data.users);
      return {
        ...state,
        asyncData: action.payload.temp.data.users,
      }
    }
  },
  effects: {
    *getData(action, {call, put}){
      console.log('hah ,you involed getData function !')
      const temp = yield call(counterService.query, {});
      yield put({
        type: 'changeData',
        payload:{
          temp
        }
      });
    }
  },
  // subscription: {
  //     setup({ dispatch, history}){
  //       return history.listen( ({pathname, query} ) =>{
  //         if(pathname === '/') {
  //           dispatch({
  //             type: 'getData',
  //             payload: {
  //               txt: 'hello, dva'
  //             }
  //           })
  //         }
  //       })
  //     },
  //   }
};
